<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
    <style>
        .layui-table-grid-down {
            display: none;
        }
        .layui-table td, .layui-table th {
            min-width: 40px;
        }
        .layui-table-cell {
            padding: 0px 5px;
        }
    </style>
</head>

<body class='labelAuto'>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">财务管理</a>
            <a>
                <cite>工人工资汇总</cite>
            </a>
        </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid labelAuto">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body search">
                    <form action="" class="layui-form">
                        <div class="layui-item">
                            <div class="layui-inline" style="">
                                <label class="layui-form-label">日期</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" id="startTime" placeholder="" >
                                </div>
                                <div class="layui-input-block" style="width:10px;text-align: center">
                                    -
                                </div>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" id="endTime" placeholder="">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">项目</label>
                                <div class="layui-input-block">
                                    <select name="" id="projectId">
                                        <option value="0">-</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">工种</label>
                                <div class="layui-input-block">
                                    <select name="" id="professionId">
                                        <option value="">-</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">搜索</label>
                                <div class="layui-input-block">
                                    <input type="search" autocomplete="off" class="layui-input" id='searchInput'>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn search" type="button" id='search'>
                                    <i class="iconfont layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body tableCardBody">
                    <table class="layui-table" id="lists" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script src="../statics/plugins/layui-v2.5.5/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/javascript">
    layui.use(['form', 'layer', "table", "laydate"], function () {
        var table = layui.table;
        var laydate = layui.laydate;
        var form = layui.form;
        var layer = layui.layer;
        $('#projectId').empty().html(projectSelect());
        $('#professionId').empty().html(professionSelect());
        form.render();
        laydate.render({
            elem: '#startTime'
            , type: 'date'
            , value: ''
            , max: 0
        });
        laydate.render({
            elem: '#endTime'
            , type: 'date'
            , value: ''
            , max: 0
        });
        // 异步数据读取
        var data1 = [];
        var dayValueLists = [];
        var lists = table.render({
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '人员列表',
            height: 'full-150',
            contentType: "application/json; charset=utf-8",
            url: url + '/finance/wage/managerWages', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count": res.data.count
                };
            },
            method: 'POST',
            where: {
                projectId: 0,
                professionId: '',
                search: "",
                startTime:'',
                endTime:'',
                token: getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: ['filter',{title: '导出', layEvent: 'excel', icon: 'layui-icon-export'}],
            page: true, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [
                [ //表头
                    {field: '', title: '编号', type: 'numbers', width: '44',totalRowText:"合计"},
                    {field: 'projectName', title: '项目', width: '120'},
                    {field: 'jobNumber', title: '工号', width: '55'},
                    {field: 'name', title: '姓名', width: '60'},
                    {field: 'professionName', title: '工种', width: '70'},
                    {
                        title: '状态', width: '50', templet: function (d) {
                            return `${d.status == 1 ? "在岗" : d.status == 2 ? "待岗" : d.status == 3 ? "请假" : "离职"}`
                        }
                    },
                    {
                        field: 'income', title: '收入汇总', width: '90',style:'color:darkcyan;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';
                            for (var i in wage) {
                                zj = Number(zj) + (wage[i].workmanAccounts) + (wage[i].handymanAccounts) + (wage[i].attendance) * (d.dayValue);
                            }
                            return zj;
                        }
                    },
                    {
                        field: 'workmanAccounts', title: '施工分账', width: '90',style:'color:darkcyan;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';
                            for (var i in wage) {
                                zj = Number(zj) + Number(wage[i].workmanAccounts);
                            }
                            return zj;
                        }
                    },
                    {
                        field: 'handymanAccounts', title: '杂工分账', width: '90',style:'color:darkcyan;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';
                            for (var i in wage) {
                                zj = Number(zj) + Number(wage[i].handymanAccounts);
                            }
                            return zj;
                        }
                    },
                    {
                        field: 'attendance', title: '考勤工资', width: '90',style:'color:darkcyan;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';
                            for (var i in wage) {
                                zj = Number(zj) + Number(wage[i].attendance) * Number(d.dayValue);
                            }
                            return zj;
                        }
                    },
                    {
                        field: 'materialAccounts', title: '材料费', width: '70',style:'color:lightcoral;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';

                            for (var i in wage) {
                                zj = Number(zj) + Number(wage[i].materialAccounts);
                            }
                            return zj;
                        }
                    },
                    {
                        field: 'loan', title: '借款', width: '70',style:'color:lightcoral;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';
                            for (var i in wage) {
                                zj = Number(zj) + Number(wage[i].loan);
                            }
                            return zj;
                        }
                    },
                    {
                        field: 'livingRecharge', title: '生活费充值', width: '80',style:'color:lightcoral;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';
                            for (var i in wage) {
                                zj = Number(zj) + Number(wage[i].livingRecharge);
                            }
                            return zj;
                        }
                    },
                    {
                        field: 'livingRefund', title: '生活费退款', width: '80',style:'color:lightcoral;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';
                            for (var i in wage) {
                                zj = Number(zj) + Number(wage[i].livingRefund);
                            }
                            return zj;
                        }
                    },
                    {
                        field: 'balance', title: '结余', width: '90',style:'color:darkcyan;font-weight: 700;',totalRowText:'读取中...', templet: function (d) {
                            var wage = d.wage;
                            var zj = '';
                            for (var i in wage) {
                                zj = Number(zj) + ((wage[i].workmanAccounts) + (wage[i].handymanAccounts) + (wage[i].attendance) * (d.dayValue) - (wage[i].materialAccounts) - (wage[i].livingRecharge) + (wage[i].livingRefund) - (wage[i].loan));
                            }
                            if (zj<0){
                                return `<span style="color:lightcoral;font-weight: 700;">${zj}</span>`
                            }else{
                                return `<span style="color:darkcyan;font-weight: 700;">${zj}</span>`;
                            }
                        }
                    },
                    {
                        field: '', title: '操作', width: '75', templet(d) {
                            return html =`<button class='layui-btn' lay-event='info' data-id=${d.id}>详情</button>`;
                        }
                    }
                ]
            ],
            totalRow:true,
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            done: function(res, curr, count){
                data1 = [];
                dayValueLists = [];
                for(var i=0;i< res.data.length;i++){
                    data1.push(res.data[i].wage)
                    dayValueLists.push(res.data[i].dayValue);
                }
                totalRow();
                permission();
            }
        });
        table.on('tool(test)', function (data) {
            if (data.event === 'info') {
                localStorage.setItem('employeeName',data.data.name);
                window.location.href = "employeeWageInfo.html?id="+data.data.id;
            }
        });

        table.on('toolbar(test)', function (data) {
            if(data.event == 'excel'){
                layer.open({
                    type: 1,
                    title: "导出",
                    area: ["40%", '30%'],
                    content: `<p class='one_p'>文件正在生成中,请稍等!</p>`,
                    btn: ['关闭'],
                    skin: 'my-skin'
                });
                //工种ID
                var search = $('#searchInput').val();
                var projectId = $('#projectId').val();
                var professionId = $('#professionId').val();
                var startTime = $('input#startTime').val();
                var endTime = $('input#endTime').val();
                if (startTime !== '' && endTime !== '') {
                    var s = new Date(startTime);
                    var e = new Date(endTime);
                    if (e < s) {
                        layer.open({
                            type: 0,
                            title: "杂工分账",
                            area: ["40%", '30%'],
                            content: `请选择截止时间不能小于起始时间`,
                            btn: ['关闭'],
                            skin: 'my-skin'
                        });
                        return
                    }
                }
                $.ajax({
                    url: url + '/excel/export/managerWageLists',
                    type: 'post',
                    data: {
                        projectId: Number(projectId),
                        professionId: Number(professionId),
                        startTime:startTime,
                        endTime:endTime,
                        search: search,
                        token: getToken()
                    },
                    dataType: 'json',
                    success: function (r) {
                        if (r.code == 100000) {
                            tips('导出', r);
                        } else {
                            tips('导出', r);
                        }
                    }
                })
            }
        });

        $('body').on('click', '#search', function () {
            var search = $(this).parent().prev().find('input').val();
            var projectId = $('#projectId').val();
            var professionId = $('#professionId').val();
			var startTime = $('input#startTime').val();
			var endTime = $('input#endTime').val();
            if (startTime !== '' && endTime !== '') {
                var s = new Date(startTime);
                var e = new Date(endTime);
                if (e < s) {
                    layer.open({
                        type: 0,
                        title: "杂工分账",
                        area: ["40%", '30%'],
                        content: `请选择截止时间不能小于起始时间`,
                        btn: ['关闭'],
                        skin: 'my-skin'
                    });
                    return
                }
            }
            data1 = [];
            dayValueLists = [];
            table.reload('lists', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    projectId: projectId,
                    professionId: professionId,
                    startTime: startTime,
                    endTime: endTime,
                    search: search,
                    token: getToken()
                }
            })
        });

        function totalRow(){
            var projectId = $('#projectId').val();
            $.ajax({
                url: url + '/finance/wage/managerWageCollect',
                type: 'post',
                data: {
                    projectId: projectId,
                    professionId: $('#professionId').val(),
                    search: $('#searchInput').val(),
                    startTime:$('#startTime').val(),
                    endTime:$('#endTime').val(),
                    token: getToken()
                },
                dataType: 'json',
                success: function (r) {
                    if (r.code == 100000) {
                        for(var t in r.data){
                            $('.layui-table-total td[data-field='+t+'] div').empty().html(r.data[t]?r.data[t]:'-')
                        }
                    }
                }
            });
        }
    });
</script>

</html>